/*
 * @Author: Vorga
 * @LastEditors: Vorga
 */
#include "avl_tree.h"

void avl_t(TreeNode** root)
{
    if (NULL == *root || (NULL == (*root)->left && NULL == (*root)->right)) return;
    avl_t(&(*root)->left);
    avl_t(&(*root)->right);
    int avl = height_lt((*root)->left) - height_lt((*root)->right);
    if (avl > 1)
    {
        if (height_lt((*root)->left->left) < height_lt((*root)->left->right))
            sin_t(&(*root)->left);
        dex_t(root);
        avl_t(root);
    }
    else if (avl < -1)
    {
        if (height_lt((*root)->right->left) > height_lt((*root)->right->right))
            dex_t(&(*root)->right);
        sin_t(root);
        avl_t(root);
    }
}

void dex_t(TreeNode** root)
{
    TreeNode* A = *root, * B = A->left, * t2 = B->right;
    *root = B;
    B->right = A;
    A->left = t2;
    cal_height_lt(A);
    cal_height_lt(B);
}

void sin_t(TreeNode** root)
{
    TreeNode* A = *root, * B = A->right, * t2 = B->left;
    *root = B;
    B->left = A;
    A->right = t2;
    cal_height_lt(A);
    cal_height_lt(B);
}
